import Service from '@/api/httpAxios'
import axios from 'axios'

export interface builds {
  project_id: string
  offset?: number
  limit?: number
  QueryBuildParam?: QueryBuild
}

export interface QueryBuild {
  name: string
  state: string
  creation_time: string
}
export interface uploadAppPath {
  project_id: string
  CreateBuildRequest: createBuildRequest
}

export interface createBuildRequest {
  name: string
  description: string
  operating_system: string
  storage_location: {
    bucket_name: string
    bucket_key: string
  }
  version: string
  region: string
  vpc_id?: string
  build_type?: string
  organization?: string
  bandwidth: {
    size?: number | string
    charging_mode?: string
  }
  subnet_id?: string
  enterprise_project?: string
  user: string,
  group: string,
  user_password: string,
  operating_system_type: number
}

export interface uploadApp {
  project_id: string
  file: FormData
}

export interface updateBuildRequest {
  name: string
  description: string
  version: string
}

export interface updateApp {
  project_id: string
  build_id: string
  UpdateBuildRequestBody: updateBuildRequest
}

export interface App {
  project_id: string
  build_id: string
}

export interface uploadAppImage {
  project_id: string
  CreateBuildByImageRequest: CreateBuildByImage
}
export interface uploadOrganizatio {
  region: string
  project_id: string
  organization: any
}
export interface CreateBuildByImage {
  name: string
  description: string
  version: string
  region: string
  image_id: string
  build_type: string
}

export let controller = new AbortController()
class ApplicationService {
  uploadApplicationImage = (obj: uploadAppImage) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/image-builds`
    return Service.post<any>(url, obj.CreateBuildByImageRequest)
  }

  uploadApplicationPath = (obj: uploadAppPath) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/builds`
    return Service.post<any>(url, obj.CreateBuildRequest)
  }
  uploadOrganization = (obj: uploadOrganizatio) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/user/swr-organization`
    return Service.post<any>(url, obj)
  }
  uploadApplication = (obj: uploadApp) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/builds/upload`
    return Service.post<any>(url, obj.file, { signal: controller.signal })
  }

  deleteApplication = (obj: App) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/builds/${obj.build_id}`
    return Service.delete<any>(url)
  }

  getApplicationDetail = (obj: App) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/builds/${obj.build_id}`
    return Service.get<any>(url)
  }

  modifyApplication = (obj: updateApp) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/builds/${obj.build_id}`
    return Service.put<any>(url, obj.UpdateBuildRequestBody)
  }

  getApplications = (obj: builds) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/builds`
    return Service.get<any>(url, {
      params: {
        offset: obj.offset ? obj.offset - 1 : obj.offset,
        limit: obj.limit,
        name: obj.QueryBuildParam?.name,
        state: obj.QueryBuildParam?.state,
        creation_time: obj.QueryBuildParam?.creation_time
      }
    })
  }

  updateController = () => {
    controller = new AbortController()
  }
}

export default new ApplicationService()
